﻿
$('document').ready(function () {

    // -> Add to cart button click event handler

    $('#add-to-cart').click(function () {

        var itemCode = $('input[name=product-option]:checked').val();
        var quantity = $("#quantity").val();
        var uom = $("#uom-options").val();

        if (uom == null || typeof uom == undefined) {

            uom = $("#uom-container-code").html();

        }

        var _msg = matrixFormIsGood(quantity, itemCode);

        if (_msg == true) {

            var str = itemCode.split("::");

            var code = str[0];
            var counter = str[1];
           
            addToCart(counter, code, quantity, uom, "shopping-cart");

        } else {

            alert(_msg);

        }

    });

    // <- Add to cart


    // -> Add to wish list button click event handler

    $('#add-to-wish-list').click(function () {

        var itemCode = $('input[name=product-option]:checked').val();
        var quantity = $("#quantity").val();
        var uom = $("#uom-options").val();


        if (uom == null || typeof uom == undefined) {

            uom = $("#uom-container-code").html();

        }


        var _msg = matrixFormIsGood(quantity, itemCode);

        if (_msg == true) {
            
            var str     = itemCode.split("::");

            var code = str[0];
            var counter = str[1];

            addToCart(counter, code, quantity, uom, "wish-list");

        } else {

            alert(_msg);

        }

    });

    // <- Add to wish list

});

function matrixFormIsGood(quantity, selection) {

    var _msg = true;

    var quantityFormat = $("#quantity-format").html();
    var quantityZero   = $("#quantity-zero").html();
    var selectedNone   = $("#selected-none").html();

    if (!quantity.match("^[0-9]+$")) {

        _msg = quantityFormat;

    }

    if (quantity <= 0 || quantity == "") {

        _msg = quantityZero;

    }

    if (selection == null) {

        _msg = selectedNone;
    }


    return _msg;

}

function getProductOptionImage(item) {
    

    var localeSetting = "(!LOCALESETTING!)";
    var skinId = "(!SKINID!)";

    var param = item;

    param = param.split("::");

    $("#option-" + param[0]).attr('checked', true);

    $(".product-image-for-matrix-options").attr("src", param[1]);

    var unitOfMeasurements = param[2];
    unitOfMeasurements = unitOfMeasurements.split(",");

    var uomLength = unitOfMeasurements.length - 1;
    var uomHTML = "";

    var uom = "";

    if (uomLength == 1) {

        uom = unitOfMeasurements[0].split("+");
        uomHTML = uom[1];
        uomHTML += "<div id='uom-container-code' style='display:none'>" + uom[0] + "</div>";

    } else {

        uomHTML  = "<SELECT id='uom-options'>";
        

        for (var i = 0; i < (uomLength - 1); i++) {

            uom = unitOfMeasurements[i].split("+");
            uomHTML += "<option value = '" + uom[0] + "' >" + uom[1] + "</option>";

        }

        uomHTML += "</SELECT>";

    }

    $("#uom-container").html(uomHTML);

}


function addToCart(counter, code, quantity, uom, cartType) {

   
    var jsonText = JSON.stringify({ counter: counter, itemCode: code, quantity: quantity, uom: uom, cartType: cartType });

    $("#test").html("");

    $.ajax({
        type: "POST",
        url: "ActionService.asmx/InsertItemToCart",
        data: jsonText,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (result) {

            var _msg = result.d;
            _msg = _msg.split("::");

            if (_msg[0] == "failed") {

                var message = _msg[1].replace("\\n", "\n");

                alert(message);

            } else {

                if (cartType == "shopping-cart") {

                    window.location = "shoppingcart.aspx";

                } else {

                    window.location = "wishlist.aspx";

                }

            }

        },
        fail: function (result) {

            alert(result.Message);

        }
    });

}

function showAlert() {

}